package com.gebogebo.android.justads;

import static java.lang.String.format;

import java.util.Random;

import com.millennialmedia.android.MMAdView;

import android.app.Activity;
import android.util.Log;
import android.widget.LinearLayout;

public class MilleniaAdManager implements AdManager {
    private static final String lg = "milleniaManager";
    private static final String MILLENIA_KEY = "<TODO: add key here>";
    //random number generator to generate viewId of MilleniaAdView
    private static final Random random = new Random(System.currentTimeMillis());

    private String gender = null;
    private String maritalStatus = null;
    private int age = -1;
    private final MilleniaAdListener listener;
    
    public MilleniaAdManager(UserPreferences prefs, MilleniaAdListener listener) {
        if(prefs != null) {
            if(prefs.getUserGender() != null) {
                gender = prefs.getUserGender().toString().toLowerCase();
                Log.i(lg, format("setting gender \"%s\" in millenia manager", gender));
            }
            if(prefs.getUserMaritalStatus() != null) {
                maritalStatus = prefs.getUserMaritalStatus().toString().toLowerCase();
                Log.i(lg, format("setting marital status to \"%s\" in millenia manager", maritalStatus));
            }
            if(prefs.getAge() > 0) {
                age = prefs.getAge();
                Log.i(lg, format("setting age to \"%s\" in millenia manager", age));
            }
        }
        this.listener = listener;
    }

    public void requestAd(Activity activity, LinearLayout ll) {
        Log.i(lg, "adding millenia ads");
        addMilleniaAd(activity, ll, MMAdView.BANNER_AD_TOP, 0);//0 = refresh
        addMilleniaAd(activity, ll, MMAdView.BANNER_AD_RECTANGLE, 0);
        addMilleniaAd(activity, ll, MMAdView.BANNER_AD_BOTTOM, 0);
    }

    public int getAdsPerRequest() {
        return 3;
    }

    /**
     * adds millenia ad to given layout, with given adType and with given refresh time.
     * (refresh time isn't working as of now)
     * 
     * @param ll layout in which millenia ad is to be added
     * @param adType millenia adType to be added
     * @param refreshSecs refresh time in secs
     */
    private void addMilleniaAd(Activity activity, LinearLayout ll, String adType, int refreshSecs) {
        Log.i("activity", format("adding millenia %s type ad", adType));
        MMAdView mmAdView = new MMAdView(activity, MILLENIA_KEY, adType, refreshSecs);
        mmAdView.setId(random.nextInt(Integer.MAX_VALUE));
        mmAdView.setListener(listener);
        mmAdView.setPadding(0, ADS_PADDING, 0, ADS_PADDING);
        populateWithPreferences(mmAdView);
        ll.addView(mmAdView);
        mmAdView.fetch();
    }
    
    private void populateWithPreferences(MMAdView v) {
        Log.i(lg, format("settings gender %s, marital status %s in millenia view", gender, maritalStatus));
        v.setGender(gender);
        v.setMarital(maritalStatus);
        if(age > 0) {
            v.setAge(Integer.toString(age));
        }
    }
}
